//
//  PPSignatureSettingViewController.m
//  pickpic
//
//  Created by Jinxiao on 9/16/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPSignatureSettingViewController.h"

#import "PPNetworkService.h"

@interface PPSignatureSettingViewController () <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *textField;
@end

@implementation PPSignatureSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"签名";
    
    self.tableView.bounces = NO;

    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    _textField.text = profiles.signature;

    __weak typeof(self) weakSelf = self;
    [self setNavigationLeftTitle:@"取消" titleColor:[UIColor colorWithRGBHex:0xf2f2f2] action:^{
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];

    [self setNavigationRightTitle:@"保存" titleColor:[UIColor colorWithRGBHex:0xffdd17] action:^{
        [weakSelf saveItemDidPressed];
    }];
}

- (void)saveItemDidPressed {
    NSString *signature = _textField.text;
    if ([signature length] > 20) {

        [self.navigationController.view toastMessage:@"签名不能超过20个字哦" duration:1.f];
        return;
    }

    NSRange range = [signature rangeOfString:@" "];
    if (range.location != NSNotFound) {
        [self.navigationController.view toastMessage:@"签名不能包含空格哦" duration:1.f];
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];

    PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;
    PPUserID userID = profiles.userID;

    __weak typeof(self) weakSelf = self;
    [PPNetworkService updateProfileWithName:nil gender:nil country:nil state:nil city:nil birthday:nil signature:signature type:5 callback:^(id data, NSError *error) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        if(strongSelf == nil)
        {
            return;
        }

        [hud hide:YES];

        if(error == nil) {
            PPAccountProfiles *profiles = [PPAccountManager sharedInstance].account.profiles;

            if([profiles.userID isEqualToString:userID]) {
                profiles.signature = signature;
                [profiles.managedObjectContext saveContext];
            }
            [strongSelf.navigationController.view toastMessage:@"变更成功" duration:1.f completion:^{
                [strongSelf.navigationController popViewControllerAnimated:YES];
            }];

        } else {
            [strongSelf.navigationController.view toastMessage:error.networkPrompt];
        }

    }];
}

#pragma mark - UITextFieldViewDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }

     /*NSString *signature = [textField.text stringByReplacingCharactersInRange:range withString:string];

    UIBarButtonItem *confirmItem = self.navigationItem.rightBarButtonItem;
    if(signature.length > 0) {
        confirmItem.enabled = YES;
        confirmItem.tintColor = [UIColor colorWithRGBHex:0xffdd17];
    } else {
        confirmItem.enabled = NO;
        confirmItem.tintColor = [UIColor colorWithRGBHex:0xb2b2b2];
    }*/
    
    return YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 15;
}

@end

